from selenium import webdriver
import time, os, random, traceback


def getTP(name, t):
    q = t.find_element_by_class_name('dM0')
    name = q.get_attribute('textContent')
    if not t.is_displayed():
        print(name)
    t.click()
    time.sleep(1)
    try:
        browser.find_element_by_link_text('查看详情').click()
    except Exception:
        print("Error: " + name)
    time.sleep(1)
    picture_url = browser.get_screenshot_as_file('D:\\111\\' + str(name) + '.png')
    time.sleep(2)
    browser.back()
    time.sleep(2)


def forList(n, table):
    i = 0
    for t in table:
        i = i + 1
        if n >= i:
            continue
        try:
            q = t.find_element_by_class_name('dM0')
            name = q.get_attribute('textContent')
            getTP(name, t)
        except Exception:
            table = browser.find_elements_by_css_selector('div.cq0')
            traceback.print_exc()
            print("Error:>>>>> " + name)
            time.sleep(3)
            forList(i, table)


if __name__ == "__main__":
    browser = webdriver.Firefox(executable_path=r"C:\Program Files\Python\Python38\Scripts\geckodriver.exe",
                                log_path=r'D:\log\webdriver.log')
    browser.get("https://vip.163.com")
    # time.sleep(2)
    # # browser.find_element_by_id('lbNormal').click()
    # time.sleep(1)
    # spCode = browser.find_element_by_name('email')
    # spCode.send_keys("lmfy")
    #
    # user_name = browser.find_element_by_name('password')
    # user_name.clear()
    # user_name.send_keys("LMJ122")
    # content = browser.find_element_by_id('dologin')
    # time.sleep(1)
    # content.click()
    time.sleep(25)
    # content = browser.find_element_by_id('_mail_component_76_76')
    # content.click()
    table = browser.find_elements_by_css_selector('div.cq0')
    name = "1"
    i = 0
    forList(0, table)

#     q.text()
#

# browser.get("https://webmail.vip.163.com")
# time.sleep(2)
# browser.find_element_by_id('lbNormal').click()
# time.sleep(1)
# spCode = browser.find_element_by_id('accname')
# spCode.send_keys("chenjin@yinrenkeji.com")
#
# user_name = browser.find_element_by_id('accpwd')
# user_name.clear()
# user_name.send_keys("K,C+si.Q8D")
# content = browser.find_element_by_css_selector('button.w-button-account')
# time.sleep(1)
# content.click()
# table= browser.find_elements_by_tag_name('table')
# for t in table:
#     q=t.find_element_by_class_name('subject')
#     print(t)
#     t.click()
#     time.sleep(1)
#     picture_url=browser.get_screenshot_as_file('D:\\111\\'+q+'.png')
